<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="bean.LoginInformation" %>
<%@ page import="bean.User" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <title>学员管理系统</title>
</head>
<style>
    td, th {
        text-align: center;
    }

    #footer {
        position: absolute;
        bottom: 0;
        width: 100%;
        text-align: center;
    }
</style>
<body style="padding: 50px;">
<div>
    <button class="btn btn-primary" onclick="addItem()">新增学员</button>
    <button class="btn btn-primary" id="select">查询学员</button>
    <input type="text" placeholder="按工号查询" id="findId">
</div>
<table class="table table-responsive">
    <thead>
    <tr>
        <th>工号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>出生日期</th>
        <th>邮箱</th>
        <th>备注</th>
        <th>班级编号</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach var="user" items="${list}" varStatus="status">
        <c:if test="${status.index%2==0}">
            <tr class="info">
                <td>${user.sno}
                </td>
                <td>${user.name}
                </td>
                <td>
                    <c:if test="${user.sex=='male'}">
                        男
                    </c:if>
                    <c:if test="${user.sex!='male'}">
                        女
                    </c:if>
                </td>
                <td>${user.birthday}
                </td>
                <td>${user.mail}
                </td>
                <td>${user.remark}
                </td>
                <td>
                    ${user.classNo}
                </td>
                <td>
                    <button class="btn btn-primary" onclick="updateItem(this)">编辑</button>
                    <button class="btn btn-primary" onclick="deleteItem(this)">删除</button>
                </td>
            </tr>
        </c:if>
        <c:if test="${status.index%2!=0}">
            <tr class="warning">
                <td>${user.sno}
                </td>
                <td>${user.name}
                </td>
                <td>
                    <c:if test="${user.sex=='male'}">
                        男
                    </c:if>
                    <c:if test="${user.sex!='male'}">
                        女
                    </c:if>
                </td>
                <td>${user.birthday}
                </td>
                <td>${user.mail}
                </td>
                <td>${user.remark}
                </td>
                <td>
                        ${user.classNo}
                </td>
                <td>
                    <button class="btn btn-primary" onclick="updateItem(this)">编辑</button>
                    <button class="btn btn-primary" onclick="deleteItem(this)">删除</button>
                </td>
            </tr>
        </c:if>
    </c:forEach>
    </tbody>
</table>
<div id="footer">
    <ul class="pagination">
        <li class="previous"><a href=""> &laquo; </a></li>
        <li id="first_page"><a href="/DemoUser/main?page=0">1</a></li>
        <li id="second_page"><a href="/DemoUser/main?page=2">2</a></li>
        <li class="next"><a href="#">&raquo;</a></li>
    </ul>
</div>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>

    function addItem() {
        window.location.href = "addItem.jsp";
    }


    function updateItem(e) {
        var tr = $(e).parent().parent().get(0);
        var s;
        $(tr).each(function () {
            s = $(this).find("td:first").text();
        })
        console.log(s);
        $.ajax({
                type: "POST",
                url: "/DemoUser/GetUserBySno",
                async: false,
                data: {sno: s},
                success: function (data, status) {
                    if (status === "success") {
                        window.location.href = "editUser.jsp";
                    }
                }
            }
        )
    }

    function deleteItem(e) {
        var tr = $(e).parent().parent().get(0);
        var s;
        $(tr).each(function () {
            s = $(this).find("td:first").text();
        })
        $.ajax({
                type: "POST",
                url: "/DemoUser/delete",
                async: false,
                data: {sno: s},
                success: function (data, status) {
                    if (status === "success") {
                        $(e).parent().parent().remove();
                    }
                }
            }
        )

    }


    //查询
    $("#select").click(function () {
        //获取输入框输入的值
        var sno = $("#findId").val();
        $.ajax({
                type: "POST",
                url: "/DemoUser/GetUserBySno",
                async: false,
                data: {sno: sno},
                success: function (data, status) {
                    if (status === "success") {
                        window.location.href = "selectResult.jsp";
                    }
                }
            }
        )
    })


</script>

</html>
